package com.reny.ll.git.base_logic.api

import com.reny.ll.git.base_logic.model.BeanGankWrap
import retrofit2.Response
import java.io.IOException

val OK_GANK: Int = 100//约定的正确返回码

fun <T> Response<BeanGankWrap<T>>.process(): T?{
    try {
        val responseCode = this.code()
        val responseMessage = this.message()
        if (this.isSuccessful) {
            this.body()?.let {
                when (it.status) {
                    OK_GANK -> {
                        return it.data
                    }
                    else -> {
                        throw HttpException(it.status, it.msg)
                    }
                }
            }
            return this.body()?.data
        } else {
            throw HttpException(responseCode, responseMessage)
        }
    } catch (e: IOException) {
        throw HttpException(message = e.message)
    }
}